package com.luoyuanxiangvip.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoyuanxiangvip.common.enums.ConfigEnum;
import com.luoyuanxiangvip.common.holder.RequestHolder;
import com.luoyuanxiangvip.common.utils.IpUtil;
import com.luoyuanxiangvip.common.utils.UaUtils;
import com.luoyuanxiangvip.common.vo.OtherOption;
import com.luoyuanxiangvip.core.entity.BizLogs;
import com.luoyuanxiangvip.core.mapper.BizLogsMapper;
import com.luoyuanxiangvip.core.service.IBizLogsService;
import com.luoyuanxiangvip.core.service.IConfigService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019-06-26
 */
@Service
public class BizLogsServiceImpl extends ServiceImpl<BizLogsMapper, BizLogs> implements IBizLogsService {

    @Resource
    @Lazy
    private IConfigService iConfigService;
    /**
     * 访问日志储存
     *
     * @param agent agent
     * @return Boolean
     */
    @Override
    public Boolean saveLogs(String agent) {
        OtherOption otherOption = iConfigService.getValue(ConfigEnum.OTHER_OPTION.getKey());
        Map<String, Object> map = IpUtil.getIpAndAddress(otherOption.getAmapApiAk(), RequestHolder.getRequest());
        BizLogs logs = new BizLogs();
        logs.setAddress((map.get("province") == null ? "" :  map.get("province"))
                + (String) map.get("city"))
                .setIp((String) map.get("ip"))
                .setAgent(agent)
                .setBrowser(UaUtils.getBorderName(agent))
                .setBrowserVersion(UaUtils.getBrowserVersion(agent))
                .setOs(UaUtils.getOs(agent))
                .setOsVersion(UaUtils.getOsVersion(agent))
                .setCreateTime(new Date())
                .setUpdateTime(new Date());
        return this.save(logs);
    }
}
